package com.grape.api.v1;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.domain.Account;
import com.grape.service.IAccountService;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.dto.StatisticAccountDTO;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.grape.annotation.RepeatSubmit;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 账户表 控制器
 * @author      putao
 * @email       2628279194@qq.com
 */
@RestController
@RequestMapping("accounts")
@Api(tags = "账户接口")
@AllArgsConstructor
public class AccountController extends BaseController<Account> {

    private final IAccountService accountService;


    @GetMapping
    @ApiOperation(value = "账户分页")
    @OperateMethodName(value = "账户表列表分页", url = "account:accounts:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<Account> pageInfo = accountService.pageList(baseVo);
        List<Account> accountList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(accountList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "账户自定义分页")
    @OperateMethodName(value = "账户表列表", url = "account:accounts:listPageCustom")
    public R<?> pageListCustom(BaseVo baseVo) {
        Page<Account> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        accountService.pageListCustom(baseVo);
        PageInfo<Account> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @RepeatSubmit
    @PutMapping
    @ApiOperation(value = "账户修改")
    @OperateMethodName(value = "账户表修改", url = "account:accounts:update")
    public R<?> update(@Validated @RequestBody Account account) {
        if (!Strings.isNotBlank(account.getId())) {
            return updateNotFound(account, null);
        }
        accountService.updateData(account);
        return updateSuccess(null, null);
    }


    @RepeatSubmit
    @PostMapping
    @ApiOperation(value = "添加账户")
    @OperateMethodName(value = "添加账户表", url = "account:accounts:insert")
    public R<?> insert(@Validated @RequestBody Account account) {
        return insertSuccess(accountService.insertData(account), null);
    }


    @DeleteMapping
    @ApiOperation(value = "删除账户，可批量多个id逗号拼接")
    @OperateMethodName(value = "账户表删除,批量", url = "account:accounts:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空") @RequestParam String ids) {
        int number = accountService.deleteData(ids);
        if (number < 1) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }


    @GetMapping("detail")
    @ApiOperation(value = "账户详情")
    @OperateMethodName(value = "账户表详情", url = "account:accounts:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        Account account = accountService.detail(id);
        if (account != null) {
            return selectSuccess(account, id);
        }
        return selectNotFound(id, null);
    }

    @GetMapping("listAll")
    @ApiOperation(value = "分类全部列表")
    @OperateMethodName(value = "分类全部列表", url = "account:accounts:listAll")
    public R<?> listAll(String accountBookId) {
        List<Account> list = accountService.listAll(accountBookId);
        return CollectionUtil.isNotEmpty(list) ? selectSuccess(list, null) : selectNotFound(list, null);
    }

    @GetMapping("statistic")
    @ApiOperation(value = "统计账户信息 资产页面")
    @OperateMethodName(value = "统计账户信息 资产页面", url = "account:accounts:statistic")
    public R<StatisticAccountDTO> statistic(@NotBlank(message = "账本id不能为空") @RequestParam("accountBookId") String accountBookId) {
        StatisticAccountDTO statistic = accountService.statistic(accountBookId);
        if (statistic == null) {
            return R.selectNotFound(null, null);
        }
        return R.selectSuccess(statistic, null);
    }
}

